package com.bzchao.niu.consumer;

import com.bzchao.netty.udp.consumer.StringUdpConsumer;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

@Slf4j
public class NiuMsgConsumer extends StringUdpConsumer {

    //模拟数据库储存
    private static final BlockingQueue<String> basket = new ArrayBlockingQueue<>(100);

    @Override
    public void accept(String msg) {
        this.saveDb(msg);
    }

    /**
     * 将数据保存到数据库
     */
    private void saveDb(String content) {
        boolean offer = basket.offer(content);
        if (!offer) {
            basket.poll();
            basket.offer(content);
        }
        log.info("数据库中总条数：{}", basket.size());
    }
}
